<?php
/**
 * 后台用户控制器
 * @author yupoxiong<i@yupoxiong.com>
 */

declare (strict_types=1);

namespace app\admin\controller;

use support\Request;
use support\Response;
use app\admin\model\AdminUser;
use app\admin\traits\AdminDelTrait;
use app\admin\traits\AdminIndexTrait;
use app\admin\traits\AdminEnableTrait;
use app\admin\traits\AdminCreateDataTrait;
use app\admin\traits\AdminUpdateDataTrait;
use app\admin\service\AdminRoleService;
use app\admin\service\AdminUserService;
use app\admin\validate\AdminUserValidate;

class AdminUserController extends AdminBaseController
{

    use AdminIndexTrait;
    use AdminCreateDataTrait;
    use AdminUpdateDataTrait;
    use AdminDelTrait;
    use AdminEnableTrait;

    public function __construct()
    {
        $this->model    = new AdminUser();
        $this->validate = new AdminUserValidate();
        parent::__construct();
    }

    /**
     * 添加
     * @param Request $request
     * @param AdminUser $model
     * @param AdminUserService $service
     * @param AdminUserValidate $validate
     * @return Response
     */
    public function add(Request $request, AdminUser $model, AdminUserService $service, AdminUserValidate $validate): Response
    {
        if ($request->method() === 'POST') {
            $param = $request->all();

            $password_check = (int)setting('admin.safe.password_check');
            if ($password_check) {
                $check_result = $service->checkPasswordLevel($param['password']);
                if ($check_result !== true) {
                    return admin_error($check_result);
                }
            }



            return $this->createData($model, $validate);
        }

        $this->assign([
            'role_list' => (new AdminRoleService())->getAll(),
        ]);

        return $this->fetch();
    }

    /**
     * 修改
     * @param Request $request
     * @param AdminUser $model
     * @param AdminUserService $service
     * @param AdminUserValidate $validate
     * @return Response
     */
    public function edit(Request $request, AdminUser $model, AdminUserService $service, AdminUserValidate $validate): Response
    {
        $id   = (request())->input('id');
        $data = $model->findOrEmpty($id);
        if ($request->method() === 'POST') {
            $param = $request->all();

            $password_check = (int)setting('admin.safe.password_check');
            if ($password_check) {
                $check_result = $service->checkPasswordLevel($param['password']);
                if ($check_result !== true) {
                    return admin_error($check_result);
                }
            }

            return $this->updateData($data, $validate);
        }

        $this->assign([
            'data'            => $data,
            'role_list'       => (new AdminRoleService())->getAll(),
            'password_config' => $service->getCurrentPasswordLevel()
        ]);


        return $this->fetch('add');
    }


    /**
     * 禁用
     * @param AdminUser $model
     * @return Response
     */
    public function disable(AdminUser $model): Response
    {
        $id        = (request())->input('id');
        $has_admin = false;
        if (is_array($id)) {
            $id = array_map('intval', $id);
            if (in_array(1, $id, true) || in_array(2, $id, true)) {
                $has_admin = true;
            }
        } else if ((int)$id === 1 || (int)$id === 2) {
            $has_admin = true;
        }
        if ($has_admin) {
            return admin_error('此账号无法禁用');
        }

        $result = $model->whereIn('id', $id)->update(['status' => 0]);
        return $result ? admin_success('操作成功', [], URL_RELOAD) : admin_error();
    }

    /**
     * 个人资料
     * @param Request $request
     * @param AdminUserValidate $validate
     * @return Response
     */
    public function profile(Request $request, AdminUserValidate $validate): Response
    {
        $user = $request->user;
        if ($request->method() === 'POST') {
            $param = $request->all();

            if ($param['update_type'] === 'password') {

                $check = $validate->scene('admin_password')->check($param);
                if (!$check) {
                    return admin_error($validate->getError());
                }

                if (!password_verify($param['current_password'], base64_decode($user->password))) {
                    return admin_error('当前密码不正确');
                }
                $param['password'] = $param['new_password'];
            }

            return $user->save($param) ? admin_success('修改成功', [], URL_RELOAD) : admin_error();
        }

        $this->assign([
            'data' => $user,
        ]);
        return $this->fetch();
    }
}
